﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media.Media3D;
using System.Windows;
using System.Windows.Media;
using System.Windows.Input;

namespace Ini_Config.CustomControl
{
    public class PasswordInputDialog : Window
    {
        private PasswordBox passwordBox = new PasswordBox();
        private TextBlock errorTextBlock = new TextBlock();

        public string? Password { get; private set; } = string.Empty;


        public PasswordInputDialog(string title)
        {
            Title = title;
            ResizeMode = ResizeMode.NoResize;
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            Width = 300;
            Height = 200;

            InitializeUI();

            Loaded += (sender, e) =>
            {
                passwordBox.Focus();
            };
        }

        private void InitializeUI()
        {
            var stackPanel = new StackPanel
            {
                Margin = new Thickness(20),
                Orientation = Orientation.Vertical
            };

            var label = new Label
            {
                Content = "Enter password:"
            };

            passwordBox = new PasswordBox();
            passwordBox.KeyDown += PasswordBox_KeyDown;

            errorTextBlock = new TextBlock
            {
                Foreground = Brushes.Red,
                Visibility = Visibility.Collapsed
            };

            var button = new Button
            {
                Content = "OK",
                Margin = new Thickness(0, 10, 0, 0)
            };
            button.Click += Button_Click;

            stackPanel.Children.Add(label);
            stackPanel.Children.Add(passwordBox);
            stackPanel.Children.Add(errorTextBlock);
            stackPanel.Children.Add(button);

            Content = stackPanel;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Password = passwordBox.Password;

            if (string.IsNullOrEmpty(Password))
            {
                errorTextBlock.Text = "密码不能为空";
                errorTextBlock.Visibility = Visibility.Visible;
            }
            else
            {
                if (Password == "123456"/*Parameter.password*/) // 检查密码是否正确
                {
                    DialogResult = true;
                }
                else
                {
                    errorTextBlock.Text = "密码错误"; // 显示密码错误消息
                    errorTextBlock.Visibility = Visibility.Visible;
                    passwordBox.Clear(); // 清空密码框
                }
            }
        }
        private void PasswordBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                Button_Click(sender, e);
            }
        }
    }
}
